<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <footer th:fragment="table">
        <div class="tpl-content-wrapper">
            <div class="row-content am-cf">
                <div class="row">
                    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                        <div class="widget am-cf">
                            <div class="widget-head am-cf">
                                <div class="widget-title  am-cf">优惠券管理</div>
                            </div>
                            <div class="widget-body  am-fr">

                                <div class="am-u-sm-4 am-u-md-2 am-u-lg-2">
                                    <div class="am-form-group">
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <button type="button" class="am-btn am-btn-default am-btn-success" data-toggle="modal" data-target="#myinsertModal"
                                                        ><span class="am-icon-plus"></span> 新增
                                                </button>
                                                <button type="button" class="am-btn am-btn-default am-btn-danger"
                                                        onclick="piliangdel()"><span class="am-icon-trash-o"></span> 删除
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="am-u-sm-4 am-u-md-2 am-u-lg-2" style="margin-left: -100px">
                                    <div class="am-form-group">
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <button type="button" class="am-btn am-btn-default am-btn-primary" data-toggle="modal" data-target="#myimportModal"
                                                ><span class="am-icon-plus"></span> 导入
                                                </button>
                                                <button type="button" class="am-btn am-btn-default am-btn-primary"
                                                        onclick="exportcoupon()"><span class="am-icon-trash-o"></span> 导出
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="am-u-sm-12 am-u-md-6 am-u-lg-3" style="margin-left: 110px">
                                    <div class="am-form-group tpl-table-list-select">
                                        <select data-am-selected="{btnSize: 'sm'}" id="status" class="selectOption">
                                            <option value="all" th:selected="${status=='all'}">全部</option>
                                            <option value="used" th:selected="${status=='used'}">已使用</option>
                                            <option value="nouse" th:selected="${status=='nouse'}">未使用</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="am-u-sm-12 am-u-md-12 am-u-lg-2">
                                    <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                                        <input type="date" class="am-form-field" id="starttime" th:value="${#dates.format(starttime,'yyyy-MM-dd')}">
                                        <input type="date" class="am-form-field" id="endtime" th:value="${#dates.format(endtime,'yyyy-MM-dd')}">
                                        <input type="text" class="am-form-field" id="keyword" th:value="${keyword}">
                                        <span class="am-input-group-btn">
                                         <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search"
                                                 type="button" onclick="searchcouponbycons(1)"></button>
                                    </span>
                                    </div>
                                </div>
                                <div class="am-u-sm-12">
                                    <div style="overflow-x:scroll">
                                        <table class="am-table am-table-compact am-table-striped tpl-table-black "
                                               id="example-r" style="width: 2000px">
                                            <thead>
                                            <tr>
                                                <th>
                                                    <button onclick="reSelectdis()"
                                                            class="am-btn  am-btn-default am-btn-success tpl-table-list-field"
                                                            type="button">反选
                                                    </button>
                                                </th>
                                                <th>编号</th>
                                                <th>优惠券名称</th>
                                                <th>优惠券状态</th>
                                                <th>优惠券内容</th>
                                                <th>优惠券金额</th>
                                                <th>优惠券到期时间</th>
                                                <th>优惠券使用条件</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <tr class="gradeX" th:each="c:${coupons.list}">
                                                <td><input type="checkbox" name="coupon" th:value="${c.couponId}" class="choice"></td>
                                                <th th:text="${c.couponId}">编号</th>
                                                <th th:text="${c.couponName}">优惠券名称</th>
                                                <th th:text="${c.couponStatus}">优惠券状态</th>
                                                <th th:text="${c.couponContext}">优惠券内容</th>
                                                <th th:text="${c.couponMoney}">优惠券金额</th>
                                                <th th:text="${#dates.format(c.couponTime,'yyyy-MM-dd')}">优惠券到期时间</th>
                                                <th th:text="${c.couponCondition}">优惠券使用条件</th>
                                                <td>
                                                    <div class="tpl-table-black-operation">
                                                        <a href="javascript:;" data-toggle="modal" data-target="#myModal" th:onclick="|showdateils('${c.couponId}')|">更多信息</a>
                                                        <a href="javascript:;" th:onclick="|deletecoupon('${c.couponId}','${coupons.pageNum}')|" class="tpl-table-black-operation-del">
                                                            <i class="am-icon-trash"></i> 删除
                                                        </a>
                                                    </div>
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                    <div class="am-u-lg-12 am-cf">
                                        <div class="am-u-lg-3">
                                            跳转至:<input type="number" id="jumpto" min="1" th:max="${coupons.pages}"  class="selectOption">/ 页
                                            <span>
                                            <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search"
                                                    type="button" onclick="tosearchcouponbycons()"></button>
                                        </span>
                                        </div>

                                        <div class="am-fr">
                                            <ul class="am-pagination tpl-pagination">
                                                <!-- 首页 -->
                                                <li>
                                                    <a th:onclick="searchcouponbycons(1)">首页</a>
                                                </li>
                                                <!-- 上一页 -->
                                                <li th:if="${coupons.hasPreviousPage}">
                                                    <a th:text="上一页" th:onclick="|searchcouponbycons('${coupons.pageNum-1}')|"></a>
                                                </li>
                                                <!-- 中间页 -->
                                                <li th:each="pageNum:${#numbers.sequence(1,coupons.pages)}" th:class="${pageNum==coupons.pageNum}?'am-active':''">
                                                    <a th:text="${pageNum}" th:onclick="|searchcouponbycons('${pageNum}')|"></a>
                                                </li>
                                                <!-- 下一页 -->
                                                <li th:if="${coupons.hasNextPage}">
                                                    <a th:text="下一页" th:onclick="|searchcouponbycons('${coupons.pageNum+1}')|"></a>
                                                </li>
                                                <!-- 尾页 -->
                                                <li>
                                                    <a th:onclick="|searchcouponbycons('${coupons.pages}')|">尾页</a>
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!--导入模态框-->
            <div class="modal fade" id="myimportModal" tabindex="-1" role="dialog" aria-labelledby="myimportModalLabel" aria-hidden="true" style="margin-top: 300px">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title" id="myimportModalLabel">查看充值更多信息</h4>
                        </div>
                        请选择需要上传的文件：<input type="file" name="file" id="importfile">
                        <div class="modal-footer">
                            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="importcoupon()">提交</button>
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        </div>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal -->
            </div>

            <input type="hidden" id="allid" th:value="${ids}">
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="margin-top: 300px">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title" id="myModalLabel">查看充值更多信息</h4>
                        </div>
                            <div id="model">

                            </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        </div>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal -->
            </div>

            <div class="modal fade" id="myinsertModal" tabindex="-1" role="dialog" aria-labelledby="myinsertModalLabel" aria-hidden="true" style="margin-top: 300px">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title" id="myinsertModalLabel">查看充值更多信息</h4>
                        </div>
                        <div>
                            <form>
                            <b>&nbsp;&nbsp;新增优惠券</b><br>
                                &nbsp;&nbsp;优惠券名称：<input type="text" id="couponName" required><br>
                                &nbsp;&nbsp;优惠券内容：<input type="text" id="couponContext" required><br>
                                &nbsp;&nbsp;优惠券金额：<input type="number" id="couponMoney" required><br>
                                &nbsp;&nbsp;到期时间：<input type="date" id="couponTime" required><br>
                                &nbsp;&nbsp;使用条件：<input type="text" id="couponCondition" required><br>
                                &nbsp;&nbsp;状态：<select id="couponStatus">
                                                        <option value="未使用">未使用</option>
                                                        <option value="已使用">已使用</option>
                                                    </select>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="submit" class="btn btn-primary" data-dismiss="modal" onclick="addcoupon()">提交</button>
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        </div>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal -->
            </div>

        </div>
    </footer>
    <script type="text/javascript">

        function importcoupon() {
            var form = new FormData();
            form.append("file", document.getElementById("importfile").files[0]);
            $.ajax({
                url:"/p2p/coupon/importcoupon",
                data:form,
                type:"post",
                dataType:"text",
                processData: false,
                contentType: false,
                success:function (data) {
                    if(data==1){
                        $.ajax({
                            url:"/p2p/coupon/getcouponbycons",
                            type:"get",
                            dataType:"html",
                            success:function (data) {
                                $("#content").html(data)
                            }
                        })
                    }else{
                        alert("导入失败")
                    }
                },
            })
        }

        /*导出*/
        function exportcoupon() {
            if(choceid.length>0){
                location.href="/p2p/coupon/exportcoupon?ids="+choceid
            }else{
                alert("请先选择！")
            }
        }
        /*反选*/
        function reSelectdis() {
            var allids=$('#allid').val().split(',')
            var allid=[]
            allid.push(parseInt(allids[0].split('')[1]))
            for(var i=1;i<allids.length;i++){
               allid.push(parseInt(allids[i]))
            }
            for(var i=0;i<choceid.length;i++){
                for (var j=0;j<allid.length;j++){
                    if(allid[j]==choceid[i]){
                        allid.splice(j,1)
                    }
                }
            }
            choceid=allid
            $("input[name='coupon']").each(function () {
                $(this).prop("checked",!$(this).prop("checked"))
            })
        }
        /*使用index页面的的数组记录已勾选的id*/
        $('.choice').click(function(){
            if($(this).prop("checked")){
                if(!($(this).val() in choceid)){
                    choceid.push($(this).val())
                }
            }else if(!$(this).prop("checked")){
                for(var i = 0;i<choceid.length; i++){
                    if (choceid[i]===$(this).val()){
                        choceid.splice(i,1);
                    }
                }
            }
        })
        /*根据页码跳转到该页*/
        function tosearchcouponbycons() {
            var currentPage=$('#jumpto').val()
            searchcouponbycons(currentPage)
        }
        /*条件查询*/
        function searchcouponbycons(i) {
            var status=$('#status').val()
            var starttime=$('#starttime').val()
            var endtime=$('#endtime').val()
            var keyword=$('#keyword').val()
            var currentPage=i
            $.ajax({
                url:"/p2p/coupon/getcouponbycons",
                data:{"status":status,"starttime":starttime,"endtime":endtime,"keyword":keyword,"currentPage":currentPage},
                type:"get",
                dataType:"html",
                success:function (data) {
                    $("#content").html(data)
                }
            })
        }
        /*删除优惠券*/
        function deletecoupon(id,i) {
            if(confirm("确定要删除吗？")){
                var status=$('#status').val()
                var starttime=$('#starttime').val()
                var endtime=$('#endtime').val()
                var keyword=$('#keyword').val()
                var currentPage=i
                $.ajax({
                    url:"/p2p/coupon/deletebyid",
                    data:{"couponId":id,"status":status,"starttime":starttime,"endtime":endtime,"keyword":keyword,"currentPage":currentPage},
                    type:"get",
                    dataType:"html",
                    success:function (data) {
                        $("#content").html(data)
                    }
                })
            }
        }
        /*查看详情*/
        function showdateils(id) {
            $.ajax({
                url:"/p2p/coupon/getcoupondetailsbyid",
                data:{"couponId":id},
                type:"get",
                dataType:"json",
                success:function (data) {
                    console.log(data)
                    var s="";
                    s+="<div class=\"modal-body\">\n" +
                        "优惠券信息:<div>"+data.couponId+"</div>\n" +
                        "\n" +
                        "</div>"
                    $('#model').html(s)
                },
            })
        }
        /*添加优惠券*/
        function addcoupon() {
            var couponName=$('#couponName').val()
            var couponContext=$('#couponContext').val()
            var couponMoney=$('#couponMoney').val()
            var couponTime=$('#couponTime').val()
            var couponCondition=$('#couponCondition').val()
            var couponStatus=$('#couponStatus').val()
            $.ajax({
                url:"/p2p/coupon/addcoupon",
                data:{"couponName":couponName,"couponContext":couponContext,"couponMoney":couponMoney,"couponTime":couponTime,"couponCondition":couponCondition,"couponStatus":couponStatus},
                type:"get",
                dataType:"text",
                success:function (data) {
                    if(data==1){
                        alert("添加成功")
                    }else{
                        alert("添加失败")
                    }
                },
            })
        }
        /*批量删除*/
        function piliangdel() {
            var ids=[]
            $("input[name='coupon']").each(function () {
                if($(this).prop("checked")){
                    ids.push($(this).val())
                }
            })
            if(ids.length>0){
                if(confirm("确定要删除吗？")){
                    $.ajax({
                        url:"/p2p/coupon/piliangdel",
                        data:{"ids":ids},
                        type:"get",
                        traditional:true,
                        dataType:"text",
                        success:function (data) {
                            if(data>=1){
                                alert("删除成功！")
                            }else{
                                alert("删除失败！")
                            }
                        },
                    })
                }
            }else{
                alert("请先选择！")
            }
        }
    </script>
</head>
</html>
